/**
 * 
 */
package com.suyx.job.processing;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.springframework.stereotype.Component;

import com.suyx.job.entity.DeviceInfo;
import com.suyx.job.utils.CommandUtils;
import com.suyx.job.utils.ImagePHash;
import com.suyx.job.utils.ImageUtils;
import com.suyx.job.utils.ScreenCapture;

import lombok.extern.slf4j.Slf4j;

/**
 * @author Administrator
 *
 */
@Slf4j
@Component("readProcessing")
public class ReadProcessing {

	private static String PATH = "d:\\android\\imges\\";
	public boolean isQydBackEvent(String device){
		 ImagePHash p = new ImagePHash();
	     String image1 = "";
	     String image2 = "";
		try {
			ScreenCapture.snapshot(device,PATH,device + "qyd.png");
			ImageUtils.cutPNG(new FileInputStream(PATH + device+"qyd.png"),
		                new FileOutputStream(PATH + "qyd_isBack.png"), 595,1130,45,50);
			image1 = p.getHash(new FileInputStream(new File(PATH +"qyd_isBack.png")));
			image2 = p.getHash(new FileInputStream(new File(PATH +"qyd_flag.png")));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	     
	    int score =  p.distance(image1, image2);
	    log.info("qyd:"+score);
	    if(score < 10){
	    	return false;
	    }
		return true;
	}
	
	public void cutPNG(String device,
			           String cutPngName,
			           Integer x,Integer y,Integer width,Integer height){
		ScreenCapture.snapshot(device,PATH,device + "qyd.png");
		try {
			ImageUtils.cutPNG(new FileInputStream(PATH + device+"qyd.png"),
			            new FileOutputStream(PATH + cutPngName), x,y,width,height);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public boolean isQydBackEvent1(String device){
		 ImagePHash p = new ImagePHash();
	     String image1 = "";
	     String image2 = "";
		try {
			ScreenCapture.snapshot(device,PATH,device + "qyd.png");
			ImageUtils.cutPNG(new FileInputStream(PATH + device+"qyd.png"),
		                new FileOutputStream(PATH + "qyd_isBack1.png"), 595,1130,45,50);
			image1 = p.getHash(new FileInputStream(new File(PATH +"qyd_isBack1.png")));
			image2 = p.getHash(new FileInputStream(new File(PATH +"qyd_before_back.png")));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	     
	    int score =  p.distance(image1, image2);
	    log.info("qyd:"+score);
	    if(score < 10){
	    	return false;
	    }
		return true;
	}
	
	
	public boolean isJrttjsbBackEvent(String device){
		 ImagePHash p = new ImagePHash();
	     String image1 = "";
	     String image2 = "";
		try {
			ScreenCapture.snapshot(device,PATH,device + "jrttjsb.png");
			ImageUtils.cutPNG(new FileInputStream(PATH + device+"jrttjsb.png"),
		                new FileOutputStream(PATH + "jrttjsb_isBack.png"), 458,1279,45,40);
			image1 = p.getHash(new FileInputStream(new File(PATH +"jrttjsb_isBack.png")));
			image2 = p.getHash(new FileInputStream(new File(PATH +"jrttjsb_flag.png")));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	     
	    int score =  p.distance(image1, image2);
	    log.info("jrttjsb:"+score);
	    if(score < 10){
	    	return false;
	    }
		return true;
	}
	
	public void swipeRead(int i,DeviceInfo device ){
		if(i%2 == 1){   //是奇数
			 String cmd = "adb -s "+device.getDeviceCode() +" shell input swipe 548 553 548 270 550";
    		 CommandUtils.excuteCommand(cmd);
	    }
		String cmd = "adb -s "+device.getDeviceCode() +" shell input swipe 375 824 375 600 550";
		CommandUtils.excuteCommand(cmd);
	}
}
